package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.annotation.RequiredPermission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService service;
    @Autowired
    private IPermissionService permissionService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageResult result = service.query(qo);
        model.addAttribute("result", result);
        return "role/list";
    }
    @RequiredPermission({"角色编辑","role:input"})
    @RequestMapping("input")
    public String input(Model model,Long id){
        if (id != null){
            Role role = service.get(id);
            model.addAttribute("entity",role);
        }
        model.addAttribute("permissions",permissionService.getAll());
        return "role/input";
    }
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"角色修改","role:saveOrUpdate"})
    public  String saveOrUpdate(Role role,Long[] ids){
        service.saveOrUpdate(role,ids);
        return "redirect:/role/list.do";
    }
    @RequiredPermission({"角色删除","role:delete"})
    @RequestMapping("/delete")
    public  String delete(Long id){
        service.delete(id);
        return "redirect:/role/list.do";
    }
}
